# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
_majorver=22
_buildver=27
_loongver=0
pkgname=openjdk$_majorver-loongarch
pkgver=0.0.$_buildver.$_loongver
pkgrel=0
_gittag="jdk-$_majorver+$_buildver-ls-$_loongver"
_prevmajorver=$((_majorver - 1))
_provide_java=false
pkgdesc="Loongarch port of OpenJDK $_majorver"
url="https://github.com/loongson/jdk"
arch="loongarch64"
license="GPL-2.0-only WITH Classpath-exception-2.0"
options="sover-namecheck"
makedepends_build="
	openjdk$_prevmajorver-bootstrap
	autoconf
	bash
	gawk
	grep
	make
	zip
	zlib-dev
	"
makedepends_host="
	alsa-lib-dev
	cups-dev
	elfutils-dev
	fontconfig-dev
	freetype-dev
	giflib-dev
	lcms2-dev
	libffi-dev
	libjpeg-turbo-dev
	libx11-dev
	libxext-dev
	libxrandr-dev
	libxrender-dev
	libxt-dev
	libxtst-dev
	linux-headers
	zlib-dev
	"
makedepends="
	$makedepends_build
	$makedepends_host
	java-cacerts
	java-common
	"
subpackages="
	$pkgname-demos::noarch
	$pkgname-doc
	$pkgname-jmods::noarch
	$pkgname-jre
	$pkgname-jre-headless:jre_headless
	$pkgname-jdk
	"
$_provide_java && subpackages="$subpackages
	$pkgname-static-libs:static_libs
	"
_gtest=1.14.0
source="$pkgname-$_gittag.tar.gz::https://github.com/loongson/jdk/archive/refs/tags/$_gittag.tar.gz
	gtest-$_gtest.tar.gz::https://github.com/google/googletest/archive/v$_gtest.tar.gz
	JDK-8218814_Wformat-security.patch
	JDK-8299245_disable-tests.patch
	JDK-8324243_calloc-args.patch
	JDK-8328997_template-id.patch
	JDK-8331352_template-id.patch
	fpu_control.patch
	lfs64.patch
	"
builddir="$srcdir/jdk-${_gittag/+/-}"

provides="openjdk$_majorver-bootstrap=$pkgver-r$pkgrel"
provider_priority=$_majorver

_java_home="/usr/lib/jvm/java-$_majorver-openjdk"

ldpath="$_java_home/lib:$_java_home/lib/server"
sonameprefix="$pkgname:"

# enable running the JTReg tests in check?
# see comment in that function for explanation
_run_jtreg=${_run_jtreg:-0}
if [ $_run_jtreg -ne 0 ]; then
	makedepends="$makedepends java-jtreg"
	checkdepends="$checkdepends font-freefont xvfb-run"
fi

_jvm_variants=server

build() {
	if [ $_run_jtreg -ne 0 ]; then
		_with_jtreg="--with-jtreg=/usr/share/java/jtreg"
	else
		_with_jtreg="--with-jtreg=no"
	fi

	if [ -n "$USE_CCACHE" ]; then
		# workaround ccache being disallowed
		export PATH="/usr/bin:/bin:/sbin:/usr/sbin"
		local ccache="--enable-ccache"
	fi

	if [ -z "$JOBS" ]; then
		export JOBS=$(printf '%s\n' "$MAKEFLAGS" | sed -n -e 's/.*-j\([0-9]\+\).*/\1/p')
	fi

	# we want to build hotspot with better optimisations; it's set to this
	# (prepended) anyway, and it's huge
	export CFLAGS="$CFLAGS -O3"
	export CXXFLAGS="$CXXFLAGS -O3"

	# CFLAGS, CXXFLAGS and LDFLAGS are ignored as shown by a warning
	# in the output of ./configure unless used like such:
	#  --with-extra-cflags="$CFLAGS"
	#  --with-extra-cxxflags="$CXXFLAGS"
	#  --with-extra-ldflags="$LDFLAGS"
	# See also paragraph "Configure Control Variables" from "common/doc/building.md"
	# shellcheck disable=2097 disable=2098
	CFLAGS='' CXXFLAGS='' LDFLAGS='' \
		bash ./configure \
		--build=$CBUILD \
		--openjdk-target=$CHOST \
		--prefix="$_java_home" \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-extra-cflags="$CFLAGS" \
		--with-extra-cxxflags="$CXXFLAGS" \
		--with-extra-ldflags="$LDFLAGS" \
		--with-zlib=system \
		--with-libjpeg=system \
		--with-giflib=system \
		--with-libpng=system \
		--with-lcms=system \
		--with-jobs=${JOBS:-4} \
		--with-test-jobs=${JOBS:-4} \
		--with-native-debug-symbols=none \
		--with-gtest="$srcdir/googletest-$_gtest" \
		$ccache \
		$_with_jtreg \
		--disable-warnings-as-errors \
		--disable-precompiled-headers \
		--enable-dtrace=no \
		--with-jvm-variants=$_jvm_variants \
		--with-debug-level=release \
		--with-version-pre= \
		--with-version-build="$_buildver" \
		--with-version-opt="ls-$_loongver-alpine-r$pkgrel" \
		--with-vendor-name="Alpine" \
		--with-vendor-url="https://alpinelinux.org/" \
		--with-vendor-bug-url="https://gitlab.alpinelinux.org/alpine/aports/issues" \
		--with-vendor-vm-bug-url="https://gitlab.alpinelinux.org/alpine/aports/issues"

	MAKEFLAGS='' make jdk-image
	if $_provide_java; then
		MAKEFLAGS='' make static-libs-image
	fi
}

check() {
	# run the gtest unittest suites
	# they don't take long, DO NOT DISABLE THEM!
	# override make variable, disable -Werror
	MAKEFLAGS='' make test-hotspot-gtest

	# The jtreg tests take very, very long to finish and show some failures (9 - 12 on my machine, varying between runs)
	# I think these are not critical and can be safely ignored.
	# As the tests take too long, they are disabled by default.
	# When updating this aport please let them run at least once on your machine to see if the failure count changes.
	if [ $_run_jtreg -ne 0 ]; then
		_logfile=$( mktemp -p "$builddir" )
		MAKEFLAGS='' xvfb-run make \
			run-test-tier1 \
			run-test-tier2 \
			run-test-tier3 \
			| tee "$_logfile"
		msg "---------------------------------------"
		msg "The build log can be found at $_logfile"
		# abort the build so you may take a look at the logfile
		false
		return 1
	fi
}

package() {
	depends="$pkgname-jdk=$pkgver-r$pkgrel"

	install -Dvm644 ASSEMBLY_EXCEPTION LICENSE README.md \
		-t "$pkgdir"/$_java_home/

	cd build/linux-*-$_jvm_variants-release/images/
	cp -a jdk/* "$pkgdir"/$_java_home/
	rm -v "$pkgdir"/$_java_home/lib/src.zip

	if $_provide_java; then
		local libstaticdir="lib/static/linux-loongarch64/musl"
		mkdir -p "$pkgdir"/$_java_home/$libstaticdir
		cp -a static-libs/lib/* "$pkgdir"/$_java_home/$libstaticdir/
	fi

	# symlink to shared java cacerts store
	rm -v "$pkgdir"/$_java_home/lib/security/cacerts
	ln -sv /etc/ssl/certs/java/cacerts \
		"$pkgdir"/$_java_home/lib/security/cacerts

	# symlink for java-common to work (expects jre in $_java_home/jre)
	ln -sv . "$pkgdir"/$_java_home/jre
}

jmods() {
	pkgdesc="$pkgdesc (jmods)"
	depends=

	provides="openjdk$_majorver-jmods"
	replaces="openjdk$_majorver-jmods"

	amove $_java_home/jmods
}

static_libs() {
	pkgdesc="$pkgdesc (libraries for static linking)"
	depends="$pkgname-jdk=$pkgver-r$pkgrel"

	$_provide_java && provides="java-jdk-static-libs"
	provides="$provides openjdk$_majorver-static-libs"
	replaces="openjdk$_majorver-static-libs"

	amove $_java_home/lib/static
}

jre() {
	pkgdesc="$pkgdesc (JRE)"
	depends="$pkgname-jre-headless=$pkgver-r$pkgrel"

	$_provide_java && provides="java-jre"
	provides="$provides openjdk$_majorver-jre"
	replaces="openjdk$_majorver-jre"

	amove \
		$_java_home/lib/libawt_xawt.so \
		$_java_home/lib/libfontmanager.so \
		$_java_home/lib/libjavajpeg.so \
		$_java_home/lib/libjawt.so \
		$_java_home/lib/libjsound.so \
		$_java_home/lib/liblcms.so \
		$_java_home/lib/libsplashscreen.so
}

jre_headless() {
	pkgdesc="$pkgdesc (JRE headless)"
	depends="java-common java-cacerts"

	$_provide_java && provides="java-jre-headless"
	provides="$provides openjdk$_majorver-jre-headless"
	replaces="openjdk$_majorver-jre-headless"

	amove \
		$_java_home/bin/java \
		$_java_home/bin/jfr \
		$_java_home/bin/jrunscript \
		$_java_home/bin/jwebserver \
		$_java_home/bin/keytool \
		$_java_home/bin/rmiregistry \
		$_java_home/conf \
		$_java_home/jre \
		$_java_home/release

	# Rest of the lib subdir (which were not taken by -jre subpkg).
	amove $_java_home/lib

	# ct.sym should stay in -jdk
	mkdir -p "$pkgdir"/$_java_home/lib
	mv "$subpkgdir"/$_java_home/lib/ct.sym "$pkgdir"/$_java_home/lib
}

doc() {
	default_doc

	amove $_java_home/man
}

demos() {
	pkgdesc="$pkgdesc (demos)"
	depends="$pkgname-jdk=$pkgver-r$pkgrel"

	provides="openjdk$_majorver-demos"
	replaces="openjdk$_majorver-demos"

	amove $_java_home/demo
}

jdk() {
	pkgdesc="$pkgdesc (JDK) ($_jvm_variants variant)"
	depends="$pkgname-jre=$pkgver-r$pkgrel $pkgname-jmods=$pkgver-r$pkgrel"

	$_provide_java && provides="java-jdk"
	provides="$provides openjdk$_majorver-jdk"
	replaces="openjdk$_majorver-jdk"

	amove \
		$_java_home/bin \
		$_java_home/lib \
		$_java_home/include
}

sha512sums="
e5e9ac6a11a22290688b98bc75d7ff84af0f4d146f48a0452351811a97c5d049567def2101aed1e68c521f5d7380820e04e99c50ffdb9afc98dcce960b064696  openjdk22-loongarch-jdk-22+27-ls-0.tar.gz
765c326ccc1b87a01027385e69238266e356361cd4ee3e18e3c9d137a5d11fa5d657c164d02dd1be8fe693c8e10f2b580588dbfa57d27f070e2750f50d3e662c  gtest-1.14.0.tar.gz
b2af7ec861a424e4f9ee4a968897d112cae862a76edc9f1f0df3b09e887a2debd51181ca7c72b70cd5387af896a01004c40ddb7a0a952591be580d3a00ddb8af  JDK-8218814_Wformat-security.patch
403862d6e6919f40d705c59463e9d6ecc5ac5b3f95e107a7e91f84d64068176e4abe7b63f15ba8fc9b4ab2f1ab58f3a489e07716e4c370356e40c2699e870b4f  JDK-8299245_disable-tests.patch
861ed6ff852429d86ca769f941a8b65c0d148c871b476130da483ba56eb91220ffc820c24b8c6f6403997b519c742651cb07fbd13966b5a6afaaa499dbe16035  JDK-8324243_calloc-args.patch
d7db30739f57eaca59a664bc146f3d088f34a70807c2bfcd2a9592d13d758ddbacc1e50bf873686d0b1b9c11bc4e721fc02d1dcffe3a1abec4fda6589d7f67ff  JDK-8328997_template-id.patch
820e06a8767f0fe3a319b71117c89457f6f1538a101b8db96626dfd0a601631bbb0606e72c47b4e8c66d451fdc41dd81f6be0a48e3d01bd61031ce6480f71946  JDK-8331352_template-id.patch
e1869d5a0ff74883b8289eb9feb5e919894cfcf418e0a07ce353d4ff2f678e81cbbba3bcf7b3642aefda963e9d9f2e6c33974ad4bb55da8aab1618ff98fd1fe1  fpu_control.patch
c77b6184d9eb50cbc62e188ad334bf3498eec4955868195aec5b71311e785bb350c667c66ad9771b98f3d12b798f6a4f51d1e7e0718a1c254f6dde50750c09cb  lfs64.patch
"
